// edit 2017-07-12
$(function () {

    // input焦点移入
    foucs_input();

    clickAgree();

    // input输入
    $(".input_t").bind("input propertychange", function () {
        var number = null;
        if (parseInt($(this).attr("data-int")) == 1) {
            number = saveIntNum($(this).val());
        } else {
            number = saveFloatNum($(this).val());
        }
        $(this).val(isNaN(number) ? 0 : number);
    });

    //全选
    $("#checkAll").click(function () {
        var nType = $(this).attr("checked");
        checkCheckBox($(this), nType == "checked" ? false : true);
        checkCheckBox(".repaymentType", nType == "checked" ? false : true);
    });

    $(".repaymentType").click(function () {
        var nType = $(this).attr("checked");
        var input = $("input.repaymentType");
        var checked = $("input.repaymentType:checked");
        if (nType == "checked") {
            checkCheckBox($(this), false);
            checkCheckBox("#checkAll", false);
        } else {
            checkCheckBox($(this), true);
            var length = $(checked).length;
            if (length === 3) {
                checkCheckBox("#checkAll", true);
            }
            if (length >= 1) {
                $("#repayErrorTr").hide();
            }
        }
    });

    // 判断值
    $(".limitMoney,.tenderMoney").blur(function () {
        newCheckMoney()
    });
    $(".productDate").blur(function () {
        newCheckDate()
    });
    $(".expectedRate").blur(function () {
        newCheckRate()
    });

    // 提交
    $("#addForm").submit(function () {

        if (!newCheckMoney()) return false;
        if (!newCheckDate()) return false;
        if (!newCheckRate()) return false;

        // if (!checkRepaymentType()) return false;

        if (parseInt($("#agree").val()) !== 1) {
            layer.alert("您还未阅读并同意相关协议", {icon: 0, title: '温馨提醒'});
            return false;
        }
        $(".submitBtn").attr("disabled", true); // 防止重复提交
        return ajax_post($(this));
    });

    $("document").ajaxStop(function () {
        $(".submitBtn").attr("disabled", false)
    });

    // 展示红包
    $(".checkPack").bind("click", function () {
        var num = parseInt($("#countCard").val());
        if (num <= 0) {
            layer.alert("暂无可以用红包！");
            return false;
        }
        cardShow();
    });

});

// 返回利率
function returnDate(date) {
    date = (date * 30);
    var rate;
    if(date<=0){
        rate = 8.5
    }else if (date <= 30 && date > 0) {
        rate = 8.5;
    } else if (date <= 60&& date > 0) {
        rate = 9.5;
    } else if (date <= 90&& date > 0) {
        rate = 10.0;
    }else if (date <= 120&& date > 0) {
        rate = 11.0;
    }else if (date <= 150&& date > 0) {
        rate = 11.5;
    } else if (date <= 180&& date > 0) {
        rate = 12.0;
    } else if (date <= 360&& date > 181) {
        rate = 12.5;
    } else if (date > 360&& date > 0) {
        rate = 13.5;
    }
    return rate;
}

// 保留正整数
function saveIntNum(obj) {
    obj = obj.replace(/[^0-9]*/g, "");
    return parseInt(obj);
}

// 保留2位小数点
function saveFloatNum(obj) {
    obj = obj.replace(/[^\d.]/g, "");
    obj = obj.replace(/\.{2,}/g, ".");
    obj = obj.replace(/^\./g, "");
    obj = obj.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj = obj.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
    return obj;
}

function foucs_input() {
    $(".input_t").bind("focus", function () {
        $(this).parents("td").removeClass("over");
    });

}

function blur_input(id, input, radio, name) {


    var val = (
        parseInt($(id).attr("data-int")) === 1 ? parseInt($(id).val()) : parseFloat($(id).val())
    );
    var va = isNaN(val) ? 0 : val;
    $(id).val(va);
    $(input).val(va);
    if (va >= 0 && !isNaN(va)) {
        $(radio).removeAttr("checked");
        $(name).val(1);
    } else {
        $(id).parents("td").addClass("over");
        $(radio).prop("checked", "checked");
        $(name).val(0);
    }
}

function keyUps(val,input,rate,i){
    if(val==0){
        $(input).val('8.5');
    }else {
        $(input).val(returnDate(val));


    }
    $(rate).val(returnDate(val))
    $(i).text(returnDate(val))
}
function foucs_inputs(i,e){
    $(i).prop("checked",false);
    $(e).prop("checked",false);
    $("input[name='apr_status']").val("1")

}

function click_radio(id, name,i) {
    $(id).find("input[name='radio']").attr("checked", "checked");
    $(i).prop("checked", "checked");
    if(i){
        $("#aprStatus").val("0")
    }

    $(id).parents("td").siblings("td").addClass("over");
    $(id).parents("td").addClass("over");
    $(id).parents("tr").next("tr.errorTr").hide();
    if (name == "#saveAccount") {
        $(name).val(0);
    } else {
        $("input[name='" + name + "']").val(0);
    }
}

// 验证金额
function newCheckMoney() {
    var val = parseInt($("#tenderAccount").val()), minval = parseInt($("#limitAccount").val()), tr = "#moneyErrorTr",
        type = parseInt($("#tenderType").val());
    if (type === 1) {
        if (val < 50 || val > 1000000 || minval < 50 || minval > 1000000 || minval > val) {
            $(".limitMoney,.tenderMoney").addClass("onFoucs");
            $(tr).show();
            return false;
        } else {
            $(".limitMoney,.tenderMoney").removeClass("onFoucs");
            $(tr).hide();
        }
        return true;
    } else {
        return true;
    }
}

// 验证日期
function newCheckDate() {
    var tr = "#dateErrorTr",
        input = ".productDate",
        nTr = "#rateErrorTr",
        min = parseInt($("input[name='timelimit_day_first']").val()),
        max = parseInt($("input[name='timelimit_day_last']").val()),
        maxText = "#maxRate",
        minText = "#minRate",
        returnMax = returnDate($("input[name='timelimit_day_last']").val()),
        returnMin = returnDate($("input[name='timelimit_day_first']").val()),
        type = parseInt($("#timeStatus").val());
    var sTrue = (min > 0 && min <= 36 && max > 0 && max <= 36 && max >= min && type === 1);
    if (type === 1) {
        if (!sTrue) {
            $(".productDate").addClass("onFoucs");
            $(tr).show();
            $(nTr).hide();
            return false;
        } else {
            $(input).removeClass("onFoucs");
            $(tr).hide();
            $(nTr).show().find(minText).html(returnMin).siblings(maxText).html(returnMax)
        }
        return true;
    } else {
        return true;
    }
}

// 验证利率
function newCheckRate() {
    var tr = "#rateErrorTr",
        tig = "#rateErrorTig",
        input = ".expectedRate",
        min = parseFloat($("input[name='apr_first']").val()),
        max = parseFloat($("input[name='apr_last']").val()),
        type = parseInt($("#aprStatus").val()),
        maxRate = 15;
    var sTrue = (min >= 1 && min <= maxRate && max >= 1 && max <= maxRate && max >= min);
    if (type == 1) {
        if (!sTrue) {
            $(input).addClass("onFoucs");
            $(tr).show();
            $(tig).show();
            return false;
        } else {
            $(input).removeClass("onFoucs");
            $(tig).hide();
        }
        return true;
    } else {
        return true;
    }
}

// 验证还款方式
function checkRepaymentType() {
    if ($(".repaymentType:checked").length === 0) {
        $("#repayErrorTr").show();
        return false;
    } else {
        $("#repayErrorTr").hide();
        return true;
    }
}

// 全选
function checkCheckBox(id, m) {
    $(id).attr("checked", m).prop("checked", m);
}